package com.xing.demo002;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Demo002Application {
	@PersistenceUnit
	EntityManagerFactory emf;
	@PersistenceContext
	EntityManager em;
	public void init(){
	}
	@RequestMapping("/")
	public Object index() {
		CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
		CriteriaQuery<Object> query=criteriaBuilder.createQuery();
		Root<VoteRecord> root=query.from(VoteRecord.class);
		query.multiselect(root.get("groupId"),criteriaBuilder.count(root.get("userId")));
		Predicate predicate =criteriaBuilder.equal(root.get("groupId"), 2);
		query.where(predicate);
		Object singleResult = em.createQuery(query).getSingleResult();
		return singleResult;
	}
	public static void main(String[] args) {
		SpringApplication.run(Demo002Application.class, args);
	}
}
